package com.alfredo.android.appcaja.dataprovider;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.alfredo.android.appcaja.model.Orden;

public class DataProvider 
{
	private final String URL_SERVER = "http://64.76.121.154:8080/AppCajaServer/servicios";
	private static DataProvider dataProvider;
	
	private DataProvider()
	{
	}
	
	public static synchronized DataProvider getInstance()
	{
		if(dataProvider == null)
			dataProvider = new DataProvider();
		
		return dataProvider;
	}
	
	public ArrayList<Orden> getOrdenes(String estadoVerificacion, String cgi, String nroExpediente, String causanteBeneficiario)
	{
		StringBuilder params = new StringBuilder();
		
		if(estadoVerificacion != null && !estadoVerificacion.isEmpty())
			params.append("estadoVerif="+URLEncoder.encode(estadoVerificacion));
		
		if(cgi != null && !cgi.isEmpty())
			params.append("&cgi="+cgi);
		
		if(nroExpediente != null && !nroExpediente.isEmpty())
			params.append("&expediente="+nroExpediente);
		
		if(causanteBeneficiario != null && !causanteBeneficiario.isEmpty())
			params.append("&causanteBeneficiario="+causanteBeneficiario);
			
		String response = getHttpRequest(URL_SERVER+"/ordenes/buscarOrdenes?"+params.toString());
				
		ArrayList<Orden> ordenes = new ArrayList<Orden>();	
		
		try 
		{
			JSONArray responseJson = new JSONArray(response);
			
			for(int i=0; i<responseJson.length(); i++)
			{
				JSONObject ordenJson = responseJson.getJSONObject(i);
				Orden orden = new Orden();
				orden.setCgi(ordenJson.getInt("cgi"));
				orden.setNroExpediente(ordenJson.getString("nroExpediente"));
				orden.setCausanteBeneficiario(ordenJson.getString("causanteBeneficiario"));
				orden.setEstado(ordenJson.getString("estado"));
				ordenes.add(orden);
			}
		}
		catch (JSONException e) 
		{
			e.printStackTrace();
		}
		
		return ordenes;
	}
	
	private String getHttpRequest(String url)
	{
		DefaultHttpClient httpClient = new DefaultHttpClient();
		//HttpPost httpPost = new HttpPost(url);
		HttpGet httpPost = new HttpGet(url);
		//httpPost.setEntity(new UrlEncodedFormEntity(params));

		try 
		{
			HttpResponse httpResponse = httpClient.execute(httpPost);
			HttpEntity httpEntity = httpResponse.getEntity();
			InputStream inputStream = httpEntity.getContent();

			BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "ISO-8859-1"), 8);
			StringBuilder sb = new StringBuilder();
			String line = null;
			
			while ((line = reader.readLine()) != null) 
			{
				sb.append(line + "\n");
			}
			inputStream.close();

			return sb.toString();
		}
		catch (ClientProtocolException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) 
		{
			e.printStackTrace();
		}

		return "";
	}
}
